package dal

import (
	"context"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"gorm.io/gen"
	"gorm.io/gen/field"
	"mall_admin_api/biz/model"
)

type AdminRoleMenuRelationDAO struct {
	bizID int64
}

type AdminRoleMenuRelationListParams struct {
	Condition []gen.Condition
	Page      int
	Size      int
	Sort      []field.Expr
}

func newAdminRoleMenuRelationDAO(bizID int64) *AdminRoleMenuRelationDAO {
	return &AdminRoleMenuRelationDAO{
		bizID: bizID,
	}
}

func (c *AdminRoleMenuRelationDAO) MGetAdminRoleMenuRelation(ctx context.Context, ids []int64) (map[int64]*model.AdminRoleMenuRelation, error) {
	list, err := AdminRoleMenuRelation.WithContext(ctx).Where(

		AdminRoleMenuRelation.ID.In(ids...),
		AdminRoleMenuRelation.DeletedAt.IsNull(),
	).Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.MGetAdminRoleMenuRelation err: %v", err)
		return nil, err
	}
	var data = make(map[int64]*model.AdminRoleMenuRelation)
	for _, info := range list {
		data[info.ID] = info
	}
	return data, nil
}

func (c *AdminRoleMenuRelationDAO) GetAdminRoleMenuRelationByConditions(ctx context.Context, condition []gen.Condition) (*model.AdminRoleMenuRelation, error) {
	tx := AdminRoleMenuRelation.WithContext(ctx).Where(

		AdminRoleMenuRelation.DeletedAt.IsNull(),
	)
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	info, err := tx.First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleMenuRelationByConditions failed, %s", err.Error())
		return nil, err
	}
	return info, nil
}

func (c *AdminRoleMenuRelationDAO) GetAdminRoleMenuRelationList(ctx context.Context, condition []gen.Condition, page, size int) ([]*model.AdminRoleMenuRelation, error) {
	tx := AdminRoleMenuRelation.WithContext(ctx).Where(AdminRoleMenuRelation.DeletedAt.IsNull())
	if len(condition) > 0 {
		tx = tx.Where(condition...)
	}
	if page > 0 && size > 0 {
		tx = tx.Offset((page - 1) * size).Limit(size)
	}
	list, err := tx.Find()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleMenuRelationList err: %v", err)
		return nil, err
	}
	return list, nil
}

func (c *AdminRoleMenuRelationDAO) GetAdminRoleMenuRelationTotal(ctx context.Context, condition []gen.Condition) (int64, error) {
	total, err := AdminRoleMenuRelation.WithContext(ctx).Where(AdminRoleMenuRelation.DeletedAt.IsNull()).Where(condition...).Count()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleMenuRelationTotal error: %v", err)
		return 0, err
	}
	return total, nil
}

func (c *AdminRoleMenuRelationDAO) GetAdminRoleMenuRelationInfoByID(ctx context.Context, id int64) (*model.AdminRoleMenuRelation, error) {
	info, err := AdminRoleMenuRelation.WithContext(ctx).Where(AdminRoleMenuRelation.DeletedAt.IsNull()).Where(AdminRoleMenuRelation.ID.Eq(id)).First()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.GetAdminRoleMenuRelationInfoByID error: %v", err)
		return nil, err
	}
	return info, nil
}

func (c *AdminRoleMenuRelationDAO) GetListWithTotal(ctx context.Context, params *AdminRoleMenuRelationListParams) ([]*model.AdminRoleMenuRelation, int64, error) {
	tx := AdminRoleMenuRelation.WithContext(ctx).Where(AdminRoleMenuRelation.DeletedAt.IsNull())
	if len(params.Condition) > 0 {
		tx = tx.Where(params.Condition...)
	}
	if len(params.Sort) > 0 {
		tx = tx.Order(params.Sort...)
	}
	list, total, err := tx.FindByPage((params.Page-1)*params.Size, params.Size)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.AdminRoleMenuRelation.GetListWithTotal err: %v", err)
		return nil, 0, err
	}
	return list, total, nil
}

func (c *AdminRoleMenuRelationDAO) CreateAdminRoleMenuRelation(ctx context.Context, data *model.AdminRoleMenuRelation) (*model.AdminRoleMenuRelation, error) {
	if err := AdminRoleMenuRelation.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateAdminRoleMenuRelation error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdminRoleMenuRelationDAO) CreateAdminRoleMenuRelationWithTx(ctx context.Context, tx *Query, data *model.AdminRoleMenuRelation) (*model.AdminRoleMenuRelation, error) {
	if err := tx.AdminRoleMenuRelation.WithContext(ctx).Create(data); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateAdminRoleMenuRelation error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdminRoleMenuRelationDAO) BatchCreateAdminRoleMenuRelation(ctx context.Context, data []*model.AdminRoleMenuRelation) error {
	if err := AdminRoleMenuRelation.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.CreateInBatchesAdminRoleMenuRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleMenuRelationDAO) BatchCreateAdminRoleMenuRelationWithTx(ctx context.Context, tx *Query, data []*model.AdminRoleMenuRelation) ([]*model.AdminRoleMenuRelation, error) {
	if err := tx.AdminRoleMenuRelation.WithContext(ctx).CreateInBatches(data, 20); err != nil {
		hlog.CtxErrorf(ctx, "dal.BatchCreateWithTxAdminRoleMenuRelation error: %v", err)
		return nil, err
	}
	return data, nil
}

func (c *AdminRoleMenuRelationDAO) UpdateAdminRoleMenuRelation(ctx context.Context, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := AdminRoleMenuRelation.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateAdminRoleMenuRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleMenuRelationDAO) UpdateAdminRoleMenuRelationWithTx(ctx context.Context, tx *Query, condition []gen.Condition, fields []field.AssignExpr) error {
	_, err := tx.AdminRoleMenuRelation.WithContext(ctx).Where(condition...).UpdateSimple(fields...)
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.UpdateAdminRoleMenuRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleMenuRelationDAO) DeleteAdminRoleMenuRelation(ctx context.Context, condition []gen.Condition) error {
	_, err := AdminRoleMenuRelation.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteAdminRoleMenuRelation error: %v", err)
		return err
	}
	return nil
}

func (c *AdminRoleMenuRelationDAO) DeleteAdminRoleMenuRelationWithTx(ctx context.Context, tx *Query, condition []gen.Condition) error {
	_, err := tx.AdminRoleMenuRelation.WithContext(ctx).Where(condition...).Delete()
	if err != nil {
		hlog.CtxErrorf(ctx, "dal.DeleteAdminRoleMenuRelationWithTx error: %v", err)
		return err
	}
	return nil
}
